using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace DukeyFileEncoding.api {
    public class Comm {
        public static bool Download(string url, string dir) {
            GetCurrentPath(ref url, ref dir);
            WebClient client = new WebClient();
            try {
                client.DownloadFile(url, dir + url.Substring(url.LastIndexOf("/") + 1));
                client.Dispose();
                return true;
            }
            catch {
                client.Dispose();
                return false;
            }
        }

        public static bool Download(string url, string dir, string filename) {
            GetCurrentPath(ref url, ref dir);
            WebClient client = new WebClient();
            try {
                client.DownloadFile(url, dir + filename);
                client.Dispose();
                return true;
            }
            catch {
                client.Dispose();
                return false;
            }
        }

        private static void GetCurrentPath(ref string url, ref string dir) {
            try {
                if (url.Contains("?")) {
                    url = url.Remove(url.IndexOf("?"), url.Length - url.IndexOf("?"));
                }
                if (!dir.EndsWith(@"\")) {
                    dir = dir.Replace("/", "") + @"\";
                }
                Uri uri = new Uri(url);
                string str = uri.Host + @"\";
                string str2 = url.Replace("//", "");
                int index = str2.IndexOf("/");
                int num2 = str2.LastIndexOf("/");
                str2 = str2.Substring(index + 1, num2 - index).Replace("/", @"\");
                dir = dir + str + str2;
                if (!Directory.Exists(dir)) {
                    Directory.CreateDirectory(dir);
                }
            }
            catch {
            }
        }

        public static long GetFileSize(string url) {
            HttpWebResponse response = null;
            long contentLength = -1L;
            try {
                response = (HttpWebResponse)GetRequest(url).GetResponse();
                contentLength = response.ContentLength;
            }
            catch {
                return 0L;
            }
            finally {
                if (response != null) {
                    response.Close();
                }
            }
            return contentLength;
        }

        public static Encoding GetPageEncoding(string url) {
            WebResponse response = WebRequest.Create(url).GetResponse();
            string name = null;
            string str2 = response.Headers["content-type"];
            if (str2 != null) {
                str2 = str2.ToLower();
                if (str2.IndexOf("charset") > 0) {
                    name = str2.Substring(str2.IndexOf("=") + 1);
                }
            }
            if (name == null) {
                string str3;
                StreamReader reader = new StreamReader(response.GetResponseStream());
                Regex regex = new Regex("<meta.*? content=(['\"]?).*?charset=(?<code>[^\\s]+)\\1.*?>", RegexOptions.IgnoreCase);
                while ((str3 = reader.ReadLine()) != null) {
                    if (regex.IsMatch(str3)) {
                        name = regex.Match(str3).Groups["code"].Value;
                        break;
                    }
                }
            }
            Encoding aSCII = Encoding.ASCII;
            if (name != null) {
                try {
                    aSCII = Encoding.GetEncoding(name);
                }
                catch {
                    aSCII = Encoding.ASCII;
                }
            }
            try {
                response.Close();
            }
            catch (Exception) {
            }
            return aSCII;
        }

        private static HttpWebRequest GetRequest(string url) {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Credentials = CredentialCache.DefaultCredentials;
            return request;
        }

        public static string Html(string strLink, Encoding encoding) {
            string str;
            if (encoding == null) {
                encoding = GetPageEncoding(strLink);
            }
            try {
                WebResponse response = WebRequest.Create(strLink).GetResponse();
                using (Stream stream = response.GetResponseStream()) {
                    using (StreamReader reader = new StreamReader(stream, encoding)) {
                        str = reader.ReadToEnd();
                        reader.Close();
                        stream.Close();
                        response.Close();
                    }
                    return str;
                }
            }
            catch {
                str = "";
            }
            return str;
        }

        public static void WriteFile(string Path, string Strings, Encoding encode) {
            if (!System.IO.File.Exists(Path)) {
                System.IO.File.Create(Path).Close();
            }
            StreamWriter writer = new StreamWriter(Path, false, encode);
            writer.Write(Strings);
            writer.Close();
            writer.Dispose();
        }
    }
}

